package project.innovation.mooddiagnosis.service.impl;

import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import project.innovation.mooddiagnosis.service.TimetampsService;

import java.io.FileWriter;
import java.io.IOException;

class DataPoint{
    private double id;
    private double start;
    private double end;

    public DataPoint(double id, double start, double end) {
        this.id = id;
        this.start = start;
        this.end = end;
    }
}

@Service
public class TimetampsServiceImpl implements TimetampsService {

    @Value("${timetampPath}")
    private String timetampsPath;

    private static final Logger logger = LoggerFactory.getLogger(TimetampsServiceImpl.class);

    public void saveTimetamps(double[] timetamps, String filename) throws IOException {
        Gson gson = new Gson();
        DataPoint[] regularTimetamps = new DataPoint[timetamps.length-1];
        for (int i = 0; i < timetamps.length-1; i++) {
            regularTimetamps[i] = new DataPoint(i,timetamps[i],timetamps[i+1]);
        }

        String json = gson.toJson(regularTimetamps);

        String filePath = timetampsPath + filename;
        try (FileWriter fw = new FileWriter(filePath)) {
            fw.write(json);
            logger.info("Successfully wrote timetamps to {}", filePath);
        }
    }
}
